//
//  UpdateConstraintsViewController.m
//  Masory使用
//
//  Created by 周成龙 on 16/10/26.
//  Copyright © 2016年 ZCL. All rights reserved.
//

#import "AnimatedViewController.h"

#import "Masonry.h"

@interface AnimatedViewController ()
@end

@implementation AnimatedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

- (void)initView {
    AnimatedView *bgView = [[AnimatedView alloc] init];
    bgView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(64, 0, 0, 0));
    }];
}

@end



//********************************************  AnimatedView
@implementation AnimatedView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (!self) return nil;
    
    [self initView];
    
    return self;
}

- (void)initView {
    
    UIView *superview = self;
    
    UIView *greenView = UIView.new;
    greenView.backgroundColor = UIColor.greenColor;
    greenView.layer.borderColor = UIColor.blackColor.CGColor;
    greenView.layer.borderWidth = 2;
    [superview addSubview:greenView];
    
    UIView *redView = UIView.new;
    redView.backgroundColor = UIColor.redColor;
    redView.layer.borderColor = UIColor.blackColor.CGColor;
    redView.layer.borderWidth = 2;
    [superview addSubview:redView];
    
    UIView *blueView = UIView.new;
    blueView.backgroundColor = UIColor.blueColor;
    blueView.layer.borderColor = UIColor.blackColor.CGColor;
    blueView.layer.borderWidth = 2;
    [superview addSubview:blueView];
    
    int padding = self.padding = 10;
    
    UIEdgeInsets paddingInsets = UIEdgeInsetsMake(self.padding, self.padding, self.padding, self.padding);
    self.animatableConstraints = [NSMutableArray array];
    
    [greenView mas_makeConstraints:^(MASConstraintMaker *make) {
        [self.animatableConstraints addObjectsFromArray:@[
          make.edges.equalTo(superview).insets(paddingInsets).priorityLow(),
          make.bottom.equalTo(blueView.mas_top).offset(-padding)
          ]];
        make.size.equalTo(redView);
        make.height.equalTo(blueView.mas_height);
    }];
    
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        [self.animatableConstraints addObjectsFromArray:@[
          make.edges.equalTo(superview).insets(paddingInsets).priorityLow(),
          make.left.equalTo(greenView.mas_right).offset(padding),
          make.bottom.equalTo(blueView.mas_top).offset(-padding)
          ]];
        make.size.equalTo(greenView);
        make.height.equalTo(blueView.mas_height);
    }];
    
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        [self.animatableConstraints addObjectsFromArray:@[
          make.edges.equalTo(superview).insets(paddingInsets).priorityLow()
          ]];
        make.height.equalTo(@[greenView, redView]);
    }];
}

- (void)didMoveToWindow {
    [self layoutIfNeeded];
    if (self.window) {
        [self animateWithInvertedInsets:NO];
    }
}

- (void)willMoveToWindow:(UIWindow *)newWindow {
    self.animating = newWindow != nil;
}

- (void)animateWithInvertedInsets:(BOOL)invertedInsets {
    if (!self.animating) return;
    
    int padding = invertedInsets? 100: self.padding;
    UIEdgeInsets paddingInsets = UIEdgeInsetsMake(padding, padding, padding, padding);
    
    for (MASConstraint *containt in self.animatableConstraints) {
        containt.insets = paddingInsets;
    }
    
    // 做动画一定要更新约束
    [UIView animateWithDuration:1 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self animateWithInvertedInsets:!invertedInsets];
    }];
}

@end
